﻿using System;
using System.Text;
using Quantum.Core;
using RabbitMQ.Client;

namespace Quantum.RabbitMq
{
    public class Publisher : RabbitMqConnetion, IPublishBehavior
    {
        public void Publish(string message)
        {
            using (var channel = Connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "broadcast", type: "fanout");
                var body = Encoding.UTF8.GetBytes(message);
                channel.BasicPublish(exchange: "broadcast",
                    routingKey: "",
                    basicProperties: null,
                    body: body);
                Console.WriteLine(" [x] Sent {0}", message);
            }

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }

        public void Broadcast<TOut>(Outgoing<TOut> message)
        {
            Publish(JsonFormatter.SerializeObject(message));
        }
    }
}
